package com.vay.de.dang.online.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.vay.de.dang.online.R;
import com.vay.de.dang.online.entiy.CustomerEntity;
import com.vay.de.dang.online.ui.BaseFrag;
import com.vay.de.dang.online.ui.apply.PhoneAuthActivity;
import com.vay.de.dang.online.util.CallerUtil;
import com.vay.de.dang.online.util.VnContext;
import com.vay.de.dang.online.util.VnUIManager;
import com.vay.de.dang.online.databinding.FragmentMineBinding;
import com.vay.de.dang.online.view.DialogConfirm;
import com.vay.de.dang.online.view.DialogUnsubscribe;
import com.vay.de.dang.online.web.LoginApi;
import com.vay.de.dang.online.web.ReqCallback;
import com.vay.de.dang.online.web.ReqResult;

import cc.taylorzhang.singleclick.SingleClickUtil;

public class TabMyFragment extends BaseFrag {

    private FragmentMineBinding binding;

    public static TabMyFragment newInstance() {
        Bundle args = new Bundle();
        TabMyFragment fragment = new TabMyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentMineBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        binding.userName.setText(VnContext.instance().getPhone());

        SingleClickUtil.onSingleClick(binding.allLayout, view -> {
            Intent intent = new Intent();
            intent.setClass(getActivity(), OrderListActivity.class);
            intent.putExtra("fromType", 0);
            startActivity(intent);
        });

        SingleClickUtil.onSingleClick(binding.pendingLayout, view -> {
            Intent intent = new Intent();
            intent.setClass(getActivity(), OrderListActivity.class);
            intent.putExtra("fromType", 1);
            startActivity(intent);
        });

        SingleClickUtil.onSingleClick(binding.repaymentLayout, view -> {
            Intent intent = new Intent();
            intent.setClass(getActivity(), OrderListActivity.class);
            intent.putExtra("fromType", 2);
            startActivity(intent);
        });

        SingleClickUtil.onSingleClick(binding.helpLayout, view -> {
            VnUIManager.nextActivity(getActivity(), HelperCenterActivity.class);
        });

        SingleClickUtil.onSingleClick(binding.feedbackLayout, view -> {
            VnUIManager.nextActivity(getActivity(), FeedbackActivity.class);
        });

        SingleClickUtil.onSingleClick(binding.aboutLayout, view -> {
            VnUIManager.nextActivity(getActivity(), AboutActivity.class);
        });

        SingleClickUtil.onSingleClick(binding.privacyLayout, view -> {
            VnUIManager.nextActivity(getActivity(), PrivacyActivity.class);
        });

        SingleClickUtil.onSingleClick(binding.customLayout, view -> {
            LoginApi.queryServicePhone(new ReqCallback(getActivity()) {
                @Override
                public void callback(ReqResult value) {
                    if (value.isOk()) {
                        CustomerEntity customer = (CustomerEntity) value.result;
                        new DialogConfirm(getActivity())
                                .setTitleText(R.string.login_service_title)
                                .setHintText(R.string.login_service_hint)
                                .setContentText(customer.getCustomerTelephone() + "\n" + customer.getCustomerEmail())
                                .showConfirm(() -> {
                                    CallerUtil.call(getActivity(), customer.getCustomerTelephone());
                                });
                    }
                }
            }.showDialog(true));
        });

        SingleClickUtil.onSingleClick(binding.cancelAccountLayout, view -> {
            new DialogUnsubscribe(getActivity()).show();
        });

        SingleClickUtil.onSingleClick(binding.settingLayout, view -> {
            VnUIManager.nextActivity(getActivity(), SetActivity.class);
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}